package com.css.apps.base.ip.service;

import java.util.List;

import com.css.apps.base.ip.model.SIp;
import com.css.core.model.IServiceResult;
import com.css.core.model.ModeFactory;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Messages;
import com.css.util.StringHelper;

public class IpService {

	public static IServiceResult<?> addIp(SIp sip) {
		IServiceResult<?> res = null;
		String result = "";
		TransactionCache tx = null;

		if (sip == null || StringHelper.isEmpty(sip.getIp()) || StringHelper.isEmpty(sip.getOpenFlag())) {
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.fieldEmpty"));
		}

		try {
			tx = new TransactionCache();
			tx.save(sip);
			tx.commit();
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_OK,
					Messages.getString("systemMsg.success"), result);
		} catch (Exception ex) {
			String msg = Messages.getString("systemMsg.dbFaild");
			if (tx != null)
				tx.rollback();
			ex.printStackTrace();
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_FAILED, msg, result);
		}
		return res;
	}

	public static IServiceResult<?> delIp(String ids) {
		IServiceResult<?> res = null;
		if (StringHelper.isEmpty(ids)) {
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.dbFaild"));
			return res;
		}

		TransactionCache tx = null;
		List<String> ipIdList = StringHelper.strToList(ids);
		@SuppressWarnings("unchecked")
		java.util.List<SIp> sipList = QueryCache.idToObj(SIp.class, ipIdList);
		tx = new TransactionCache();
		try {
			for (SIp sip : sipList) {
				// SIp sip=QueryCache.get(SIp.class, uid);
				tx.delete(sip);
			}
			tx.commit();
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_OK,
					Messages.getString("systemMsg.success"));
		} catch (Exception ex) {
			if (tx != null) {
				tx.rollback();
			}
			ex.printStackTrace();
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.dbFaild"));
			return res;
		}
		return res;
	}

	public static IServiceResult<?> updIp(SIp sip) {
		IServiceResult<?> res = null;
		TransactionCache tx = null;

		if (sip == null || StringHelper.isEmpty(sip.getIp()) || StringHelper.isEmpty(sip.getOpenFlag())) {
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.fieldEmpty"));
		}

		try {
			tx = new TransactionCache();
			tx.update(sip);
			tx.commit();
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_OK,
					Messages.getString("systemMsg.success"), null);
		} catch (Exception ex) {
			if (tx != null) {
				tx.rollback();
			}
			ex.printStackTrace();
			res = ModeFactory.getModeFactory().buildNewServiceResult(IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.fieldEmpty"));
		}
		return res;
	}
}
